package controllers

/**
 * Created by yz on 2017/5/31.
 */

import command.CommandExec
import dao._
import play.api.mvc._
import tool.{FormTool, Tool}
import utils.Utils

import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global
import implicits.Implicits._
import shared.Pojo._
import tool.Pojo.CommandData

import java.io.File
import scala.collection.immutable.SeqMap
import upickle.default._
import ujson.play.PlayJson

class SearchController @Inject()(cc: ControllerComponents)(
  implicit val geneExpDao: GeneExpDao,
  implicit val genesDao: GenesDao,
  implicit val browseGeneDao: BrowseGeneDao,
  implicit val mTissueDataDao: MTissueDataDao,
  implicit val hTissueDataDao: HTissueDataDao,
  implicit val cellLineExprDao: CellLineExprDao,
  implicit val modeDao: ModeDao,

) extends AbstractController(cc) {

  def searchByGeneSymbol = Action.async { implicit request =>
    val data = FormTool.geneNameForm.bindFromRequest.get
    val geneNames = data.geneName.mySplit(",")
    genesDao.selectAll(geneNames).flatMap { rows =>
      val allGenes = rows.flatMap { row =>
        List(row.mGene, row.hGene)
      }.toList
      browseGeneDao.selectAll(allGenes)
    }.map { x =>
      val array = Utils.getArrayByTs(x)
      Ok(writeJs(array))
    }
  }

  def getOrtholog = Action.async { implicit request =>
    val data = FormTool.geneNameForm.bindFromRequest.get
    val species = FormTool.speciesForm.bindFromRequest.get.species
    val f = species match {
      case "Mmu" => genesDao.getGeneNamesByM(data.geneName)
      case "Hsa" => genesDao.getGeneNamesByH(data.geneName)
    }
    f.map { x =>
      Ok(writeJs(x))
    }
  }

}
